package com.jinbiao.quasar.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.jinbiao.quasar.annotate.OperationLog;
import com.jinbiao.quasar.model.SysConfigBackend;
import com.jinbiao.quasar.model.SysConfigFrontend;
import com.jinbiao.quasar.util.AjaxJson;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.extend.sqltoy.annotation.Db;
import org.sagacity.sqltoy.dao.SqlToyLazyDao;
import org.sagacity.sqltoy.model.EntityQuery;
import org.sagacity.sqltoy.model.Page;

import java.util.List;

@Controller
@OperationLog
public class ConfigBackendController {
    @Db
    SqlToyLazyDao dao;
    @Mapping("/config-backend/get-config-backend-list")
    public AjaxJson getConfigBackendList(int page,int pageSize,String sortBy,boolean desc,SysConfigBackend sysConfigBackend){
        String sqlTemplate = "SELECT * FROM sys_config_backend WHERE " +
                "            #[and config_item like :configItem]" +
                "            #[and item_default like :itemDefault] " +
                "            #[and item_custom like :itemCustom] " +
                "            #[and memo like :memo]" +
                " order by {} {}";
        String sql = StrUtil.format(sqlTemplate, sortBy, desc ? "desc" : "asc");
        Page<SysConfigBackend> pageResult =  dao.findPageBySql(new Page(pageSize, page), sql, BeanUtil.beanToMap(sysConfigBackend),SysConfigBackend.class);
        return AjaxJson.getSuccessData(pageResult);
    }

    @Mapping("/config-backend/delete-config-backend-by-id")
    public AjaxJson deleteConfigBackendById(SysConfigBackend sysConfigBackend){
        return AjaxJson.getSuccessData(dao.delete(sysConfigBackend));
    }

    @Mapping("/config-backend/edit-config-backend")
    public AjaxJson editConfigBackend(SysConfigBackend sysConfigBackend){
        return AjaxJson.getSuccessData(dao.saveOrUpdate(sysConfigBackend));
    }

    @Mapping("/config-backend/add-config-backend")
    public AjaxJson addConfigBackend(SysConfigBackend sysConfigBackend){
        return AjaxJson.getSuccessData(dao.saveOrUpdate(sysConfigBackend));
    }

    @Mapping("/config-backend/query-config-backend-by-id")
    public AjaxJson queryConfigBackendById(SysConfigBackend sysConfigBackend){
        List<SysConfigBackend> results = dao.findBySql("sysConfigBackendList",sysConfigBackend);
        if(CollectionUtil.isNotEmpty(results)){
            SysConfigBackend configBackend = results.get(0);
            return AjaxJson.getSuccessData(configBackend);
        }
        return AjaxJson.getSuccessData(null);
    }

    @Mapping("/public/get-config-backend-all")
    public AjaxJson getConfigBackendAll(SysConfigBackend sysConfigBackend){
        return AjaxJson.getSuccessData(dao.findBySql("sysConfigBackendList",sysConfigBackend));
    }
}
